package com.hawthorn.redis.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hawthorn.component.constant.RedisConstant;
import com.hawthorn.component.utils.common.StrMyUtil;
import com.hawthorn.entity.po.SysUserPO;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.redis.repository.UserCacheMapper;
import com.hawthorn.redis.service.user.UserCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>todo 加载用户缓存服务实现类</p>
 * @author: andyten@tom.com | xukaixing@hotmail.com
 * @created: 2021/11/11 8:48 PM
 * @version v1.0.3
 * @copyright: Copyright (c)  andyten
 */
@Service
@Slf4j
public class UserCacheServiceImpl implements UserCacheService
{
  @Autowired
  RedisMyClient redisMyClient;

  @Resource
  private UserCacheMapper userCacheMapper;

  /**
   * <p>todo 初始化方法</p>
   * @param:
   * @return: void
   * @author: andyten@tom.com | xukaixing@hotmail.com
   * @date: 2021/11/11 8:48 PM
   * @version: 1.0.1
   * Modification History:
   * Date         Author          Version            Description
   * -----------------------------------------------------------
   * 2021/11/11    andy.ten        v1.0.1             init
   */
  @Transactional(readOnly = true)
  public void init()
  {
    List<Map<String, Object>> userCache = userCacheMapper.selectUser();
    String key;
    // todo 先删除redis中的key
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_USER_PREFIX, "*"));

    for (Map user : userCache)
    {
      key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_USER_PREFIX, MyUtil.getValue(user.get("account")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_ID, MyUtil.getValue(user.get("id")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_ACCOUNT, MyUtil.getValue(user.get("account")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_PASSWORD, MyUtil.getValue(user.get("password")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_SALT, MyUtil.getValue(user.get("salt")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_USERNAME, MyUtil.getValue(user.get("user_name")));

      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_TYPE, MyUtil.getValue(user.get("user_type")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_LEVEL, MyUtil.getValue(user.get("user_level")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DEPTID, MyUtil.getValue(user.get("dept_id")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_OEMCOMPANYID, MyUtil.getValue(user.get("oem_company_id")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_COMPANYID, MyUtil.getValue(user.get("company_id")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DEPTCODE, MyUtil.getValue(user.get("dept_code")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_MENULAYOUT, MyUtil.getValue(user.get("menu_layout")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_THEMETYPE, MyUtil.getValue(user.get("theme_type")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DISPLAYTYPE, MyUtil.getValue(user.get("display_type")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_ALERTTYPE, MyUtil.getValue(user.get("alert_type")));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_STATUS, MyUtil.getValue(user.get("status")));

      // 默认是否重新载入权限 1:重新载入；0：不载入
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_GRANTLOAD, "0");
      // 默认登录语言类型
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_LANGUAGE, "1");
      // 设置登录ip
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_LOGIN_IP, "127.0.0.1");
    }
    log.info("====== load userdata to redis success ======");
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void saveUser(SysUserPO sysUserPO)
  {
    String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_USER_PREFIX, MyUtil.getValue(sysUserPO.getAccount()));

    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_ID, sysUserPO.getId().toString());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_ACCOUNT, sysUserPO.getAccount());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_PASSWORD, sysUserPO.getPassword());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_SALT, sysUserPO.getSalt());
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_USERNAME, sysUserPO.getUserName());

    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_TYPE, MyUtil.getValue(sysUserPO.getUserType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_LEVEL, MyUtil.getValue(sysUserPO.getUserLevel()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DEPTID, MyUtil.getValue(sysUserPO.getDeptId()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_OEMCOMPANYID, MyUtil.getValue(sysUserPO.getOemCompanyId()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_COMPANYID, MyUtil.getValue(sysUserPO.getCompanyId()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DEPTCODE, MyUtil.getValue(sysUserPO.getDeptCode()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_MENULAYOUT, MyUtil.getValue(sysUserPO.getMenuLayout()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_THEMETYPE, MyUtil.getValue(sysUserPO.getThemeType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DISPLAYTYPE, MyUtil.getValue(sysUserPO.getDisplayType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_ALERTTYPE, MyUtil.getValue(sysUserPO.getAlertType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_STATUS, MyUtil.getValue(sysUserPO.getStatus()));
    // 默认是否重新载入权限 1:重新载入；0：不载入
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_GRANTLOAD, "0");
    // 默认登录语言类型
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_LANGUAGE, "1");
    // 设置登录ip
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_LOGIN_IP, "127.0.0.1");
    log.info("====== insert userdata to redis success ======");
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void updateUser(SysUserPO sysUserPO)
  {
    // todo: 更新用户缓存
    String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_USER_PREFIX, MyUtil.getValue(sysUserPO.getAccount()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_USERNAME, MyUtil.getValue(sysUserPO.getUserName()));

    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_TYPE, MyUtil.getValue(sysUserPO.getUserType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_LEVEL, MyUtil.getValue(sysUserPO.getUserLevel()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DEPTID, MyUtil.getValue(sysUserPO.getDeptId()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_OEMCOMPANYID, MyUtil.getValue(sysUserPO.getOemCompanyId()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_COMPANYID, MyUtil.getValue(sysUserPO.getCompanyId()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DEPTCODE, MyUtil.getValue(sysUserPO.getDeptCode()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_MENULAYOUT, MyUtil.getValue(sysUserPO.getMenuLayout()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_THEMETYPE, MyUtil.getValue(sysUserPO.getThemeType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DISPLAYTYPE, MyUtil.getValue(sysUserPO.getDisplayType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_ALERTTYPE, MyUtil.getValue(sysUserPO.getAlertType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_STATUS, MyUtil.getValue(sysUserPO.getStatus()));

    log.info("====== update user data to redis successfully ======");
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void updateUserByAccount(String account)
  {
    // todo: 更新用户缓存
    String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_USER_PREFIX, account);
    SysUserPO sysUserPO = userCacheMapper.selectOne(new QueryWrapper<SysUserPO>().eq("account", account));

    if (sysUserPO != null)
    {
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_USERNAME, MyUtil.getValue(sysUserPO.getUserName()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_TYPE, MyUtil.getValue(sysUserPO.getUserType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_LEVEL, MyUtil.getValue(sysUserPO.getUserLevel()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DEPTID, MyUtil.getValue(sysUserPO.getDeptId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_OEMCOMPANYID, MyUtil.getValue(sysUserPO.getOemCompanyId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_COMPANYID, MyUtil.getValue(sysUserPO.getCompanyId()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DEPTCODE, MyUtil.getValue(sysUserPO.getDeptCode()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_MENULAYOUT, MyUtil.getValue(sysUserPO.getMenuLayout()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_THEMETYPE, MyUtil.getValue(sysUserPO.getThemeType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_DISPLAYTYPE, MyUtil.getValue(sysUserPO.getDisplayType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_ALERTTYPE, MyUtil.getValue(sysUserPO.getAlertType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_USER_STATUS, MyUtil.getValue(sysUserPO.getStatus()));

      log.info("====== update user data to redis successfully [" + account + "] ======");
    }

  }

}
